#include "panel_main_window.h"
#include "ui_panel_main_window.h"
#include <QFile>
#include <QTextStream>
#include <QButtonGroup>
#include <QDebug>
#include <QRandomGenerator>
#include "database_manager.h"
#include <QSqlQuery>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "search_tree_widget.h"

PanelMainWindow::PanelMainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui_(new Ui::PanelMainWindow),
      overview_view_(nullptr),
      alarm_view_(nullptr),
      data_view_(nullptr),
      config_view_(nullptr),
      yk_comparison_view_(nullptr),
      state_info_(new StateInfo()),
      dealer_handler_(nullptr),
      search_tree_(new SearchTreeWidget)
{
    ui_->setupUi(this);

    // 创建平行四边形按钮
    ui_->btnOverview->setFixedSize(100, 30);
    ui_->btnOverview->setEnabled(true);
    ui_->btnAlarm->setFixedSize(100, 30);
    ui_->btnAlarm->setEnabled(true);
    ui_->btnData->setFixedSize(100, 30);
    ui_->btnData->setEnabled(true);
    ui_->btnConfig->setFixedSize(100, 30);
    ui_->btnConfig->setEnabled(true);

    // 设置按钮互斥
    ui_->btnOverview->setCheckable(true);
    ui_->btnAlarm->setCheckable(true);
    ui_->btnData->setCheckable(true);
    ui_->btnConfig->setCheckable(true);
    ui_->ykComp->setCheckable(true);
    // 设置按钮组
    QButtonGroup *buttonGroup = new QButtonGroup(this);
    buttonGroup->addButton(ui_->btnOverview);
    buttonGroup->addButton(ui_->btnAlarm);
    buttonGroup->addButton(ui_->btnData);
    buttonGroup->addButton(ui_->btnConfig);
    buttonGroup->addButton(ui_->ykComp);

    // 默认选中第一个按钮
    ui_->btnOverview->setChecked(true);

    // 加载样式表
    QFile styleFile(":/styles/styles.qss");
    if (styleFile.open(QFile::ReadOnly | QFile::Text))
    {
        QTextStream in(&styleFile);
        in.setCodec("UTF-8");
        QString style = in.readAll();
        this->setStyleSheet(style);
        styleFile.close();
    }

    // 初始化网络尽量放在第一步
    initNetwork();
    initViews();
    setupConnections();
}

void PanelMainWindow::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
}

PanelMainWindow::~PanelMainWindow()
{
    // 停止网络连接
    DealerHandler::getInstance()->stop();

    delete ui_;
    // 释放state_info_
    if (state_info_)
    {
        delete state_info_;
        state_info_ = nullptr;
    }
    // 释放state_info_vec_
    for (auto sub_state_info : state_info_vec_)
    {
        delete sub_state_info;
        sub_state_info = nullptr;
    }
    state_info_vec_.clear();
}

void PanelMainWindow::initViews()
{
    // 初始化总览视图
    overview_view_ = new OverviewView(this);
    ui_->stackedWidget->addWidget(overview_view_);
    ui_->stackedWidget->setCurrentWidget(overview_view_);

    // 初始化告警视图
    alarm_view_ = new AlarmView(search_tree_, this);
    ui_->stackedWidget->addWidget(alarm_view_);

    // 初始化数据视图
    data_view_ = new DataView(search_tree_, this);
    ui_->stackedWidget->addWidget(data_view_);

    // 初始化遥测对比视图
    yk_comparison_view_ = new YKComparison(search_tree_, this);
    ui_->stackedWidget->addWidget(yk_comparison_view_);

    // 初始化配置视图
    config_view_ = new ConfigView(search_tree_, this);
    ui_->stackedWidget->addWidget(config_view_);
}

void PanelMainWindow::initNetwork()
{
    // 获取网络管理器实例
    dealer_handler_ = DealerHandler::getInstance();

    // 连接信号槽
    connect(dealer_handler_, &DealerHandler::messageReceived, this, &PanelMainWindow::onNetworkMessageReceived);
    connect(dealer_handler_, &DealerHandler::connectionStatusChanged, this, &PanelMainWindow::onNetworkStatusChanged);
    connect(dealer_handler_, &DealerHandler::errorOccurred, this, &PanelMainWindow::onNetworkError);
}

void PanelMainWindow::onNetworkMessageReceived(const QString &method, const QString &body)
{
    // 只处理非数据查询相关的消息
    if (method == "select-frames")
    {
        return;
    }

}

void PanelMainWindow::onNetworkStatusChanged(bool connected)
{
    qDebug() << "Network status changed:" << (connected ? "Connected" : "Disconnected");
}

void PanelMainWindow::onNetworkError(const QString &error_message)
{
    qDebug() << "Network error:" << error_message;
}

void PanelMainWindow::setupConnections()
{
    connect(ui_->btnOverview, &QPushButton::clicked, this, &PanelMainWindow::onBtnOverviewClicked);
    connect(ui_->btnAlarm, &QPushButton::clicked, this, &PanelMainWindow::onBtnAlarmClicked);
    connect(ui_->btnData, &QPushButton::clicked, this, &PanelMainWindow::onBtnDataClicked);
    connect(ui_->btnConfig, &QPushButton::clicked, this, &PanelMainWindow::onBtnConfigClicked);
    connect(ui_->ykComp, &QPushButton::clicked, this, &PanelMainWindow::onBtnYKComparisonClicked);
}

void PanelMainWindow::onBtnOverviewClicked()
{
    ui_->stackedWidget->setCurrentWidget(overview_view_);
}

void PanelMainWindow::onBtnAlarmClicked()
{
    ui_->stackedWidget->setCurrentWidget(alarm_view_);
}

void PanelMainWindow::onBtnDataClicked()
{
    ui_->stackedWidget->setCurrentWidget(data_view_);
}

void PanelMainWindow::onBtnConfigClicked()
{
    ui_->stackedWidget->setCurrentWidget(config_view_);
}

void PanelMainWindow::onBtnYKComparisonClicked()
{
    ui_->stackedWidget->setCurrentWidget(yk_comparison_view_);
}
